import request from '@/utils/request'
import {ElMessage} from "element-plus";


// 获取定价参数
export function getDefaultPricingParam(otcOptionJson) {
    return request({
        url: 'pricing/otcOption/calcMarket/getCalcPricingParamByCfgId.action',
        method: 'post',
        data: otcOptionJson
    })
}
/*获取 曲线或曲面插值*/
export function calcVolSurfaceRateBase64(data) {
    return request({
        url: '/pricing/otcOption/calcMarket/calcVolSurfaceRateBase64.action',
        method: 'post',
        data: data
    })
}/*获取分红曲线插值*/
export function calcDivCurveRateBase64(data) {
    return request({
        url: '/pricing/otcOption/calcMarket/calcDivCurveRateBase64.action',
        method: 'post',
        data: data
    })
}/*获取曲线插值*/
export function calcCurveRateBase64(data) {
    return request({
        url: '/pricing/otcOption/calcMarket/calcCurveRateBase64.action',
        method: 'post',
        data: data
    })
}

/**
 * 获取交易日历  目前后台写死CHINA_EX
 * data:  {
    startDay:'YYYY-MM-DD',
    endDay:'YYYY-MM-DD'
}
 */
export function getCalendar(data) {
    return request({
        url: 'pricing/otcOption/calcMarket/getCalendar.action',
        method: 'post',
        data: data
    })
}

// 获取一个合理的交易日  目前后台写死CHINA_EX
// data:{calDay:"2022-xx-xx"}
export function getTerminationPaymentDate(date) {
    return request({
        url: 'pricing/otcOption/calcMarket/getTerminationPaymentDate.action',
        method: 'post',
        data: date
    })
}

// 获取一个合理的交易日 上一个交易日  Preceding 目前后台写死CHINA_EX
// data:{calDay:"2022-xx-xx"}
export function getFixDate(date) {
    return request({
        url: 'pricing/otcOption/calcMarket/getFixDate.action',
        method: 'post',
        data: date
    })
}

// 获取一个合理的交易日  目前后台写死CHINA_EX
// data:{calDay:"2022-xx-xx"}
export function getOffset(date) {
    return request({
        url: 'pricing/otcOption/calcMarket/getOffset.action',
        method: 'post',
        data: date
    })
}


// 试算
export function calcMarket(otcOptionJson) {
    return request({
        url: 'pricing/otcOption/calcMarket.action',
        method: 'post',
        data: otcOptionJson
    })
}


// 反算合约要素
export function calcContractParam(otcOptionJson) {
    return request({
        url: 'pricing/otcOption/calcContractParam.action',
        method: 'post',
        data: otcOptionJson
    })
}

export async function calcContractParamProxy(otcOptionJson) {
    return new Promise((resolve) => {
        calcContractParam(otcOptionJson).then((response) => {
            if (response.errorCode != 0) {
                ElMessage.warning("当前NPV:"+otcOptionJson.calcPricingParam.marketNPV+"; 无对应反算解，请调整NPV至合理范围。");
            } else {
                let result = response.result;
                if (Boolean(result)) {
                    resolve(result)
                }
            }
        })
    })
}

// 试算
export function marketNPV(otcOptionJson) {
    return request({
        url: 'pricing/otcOption/calcMarketVol.action',
        method: 'post',
        data: otcOptionJson
    })
}

// 情景试算
export function calcScene(otcOptionJson) {
    return request({
        url: 'pricing/otcOption/calcScene.action',
        method: 'post',
        data: otcOptionJson
    })
}


// 试算
export function saveCalcLogs(otcOptionJson) {
    return request({
        url: 'pricing/calcLogs/saveCalcLogs.action',
        method: 'post',
        data: otcOptionJson
    })
}


export function getInstrumentList(param) {
    return request({
        url: 'marketdata/instrument/getInstruments.action',
        method: 'get',
        params: param
    })
}

export function getDailyPrice(param) {
    return request({
        url: 'marketdata/instrument/getdailyprice.action',
        method: 'get',
        params: param
    })
}

export function getIrCurveList(param) {
    return request({
        url: '/marketdata/ircurve/queryTirCurveInfoByPage.action',
        method: 'get',
        params: param
    })
}

export function getCalcLogs(param) {
    return request({
        url: '/pricing/calcLogs/queryCalcLogs.action',
        method: 'get',
        params: param
    })
}

export function deleteCalcLogs(param) {
    return request({
        url: '/pricing/calcLogs/deleteCalcLogs.action',
        method: 'get',
        params: param
    })
}

// "<?xml version=\"1.0\" encoding=\"GB2312\"?><root><Parameter><KeyValues><MCPathNum DATATYPE = \"INT\" TYPE = \"SIMPLE\" >100000</MCPathNum><VarianceReductionMethod DATATYPE = \"INT\" TYPE = \"SIMPLE\" >0</VarianceReductionMethod></KeyValues></Parameter></root>"
// "<?xml version=\"1.0\" encoding=\"GB2312\"?><root><Parameter><KeyValues><GridSize DATATYPE=\"INT\" TYPE=\"SIMPLE\">256</GridSize><RangeScale DATATYPE=\"INT\" TYPE=\"SIMPLE\">10</RangeScale></KeyValues></Parameter></root>"